package littlepicture.data.tools;

import java.io.IOException;
import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.Dao;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;

public class DaoService {
	private static  DaoManager daoManager;
	
	public static synchronized DaoManager getDaoManager(){
		String daoXmlResource = "littlepicture/dao.xml";
		Reader reader;
		
		if(daoManager == null){
			try{
				reader = Resources.getResourceAsReader(daoXmlResource);
				daoManager = DaoManagerBuilder.buildDaoManager(reader);
				return daoManager;
			}catch(IOException e){
				throw new RuntimeException(
						"Unable to create DAO Manager.", e
				);
			}
		} else {
			return daoManager;
		}
	}
	
	public static Dao getDao(Class interfaceClass){
		return getDaoManager().getDao(interfaceClass);
	}
}
